const user = {
    state: {
      token: localStorage.getItem('token') || '',
      userInfo: JSON.parse(localStorage.getItem('userInfo')) || null
    },
    
    mutations: {
      SET_TOKEN: (state, token) => {
        state.token = token
        localStorage.setItem('token', token)
      },
      SET_USER_INFO: (state, userInfo) => {
        state.userInfo = userInfo
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
      },
      CLEAR_USER: (state) => {
        state.token = ''
        state.userInfo = null
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
      }
    },
    
    actions: {
      login({ commit }, userInfo) {
        return new Promise((resolve, reject) => {
          // 这里替换为实际的登录API调用
          setTimeout(() => {
            const data = {
              token: 'mock-token',
              user: {
                username: userInfo.username,
                avatar: 'https://example.com/avatar.jpg'
              }
            }
            commit('SET_TOKEN', data.token)
            commit('SET_USER_INFO', data.user)
            resolve(data)
          }, 1000)
        })
      },
      
      logout({ commit }) {
        commit('CLEAR_USER')
      }
    }
  }
  
  export default user